home *** CD-ROM | disk | FTP | other *** search
- /*************************************************************************/
- /* */
- /* DECOMPC.C Compression de fichiers source C. */
- /* V1.00 FREEWARE Christian BRUNON 20-08-1993 */
- /* */
- /* +-----------------------------------------+ */
- /* | Logiciel placé dans le domaine FREEWARE | */
- /* +-----------------------------------------+ */
- /* L'utilisation et la diffusion de ce programme, des fichiers sources */
- /* et de la documentation sont entièrement libres. */
- /* */
- /* Christian BRUNON */
- /* 30 Rue Georges Brassens */
- /* 43140 LA SEAUVE SUR SEMENE */
- /* FRANCE */
- /* */
- /* Fichier source Lattice C AmigaDOS Version 5.04 */
- /* */
- /*************************************************************************/
-
- /* Fichier INCLUDE */
- #include "CPRG:CompC.H"
-
- static int Car, /* Caractère lu dans le fichier */
- CarPrec; /* Caractère précédent lu dans le fichier */
-
- static UBYTE ListeMots[MaxMots][MaxLongMot];
-
- const UBYTE *ListeErreurs[] =
- {
- "\n",
- "Utilisation : DeCompC [-DEST] Fichier1[.K] [-DEST] [Fichier2[.K]] [...]\n",
- "\aFichier inexistant\n",
- "\aErreur en création\n",
- "\aErreur en écriture\n",
- "\aErreur en lecture\n",
- "\aPas assez de mémoire\n",
- "\a***BREAK\n"
- };
-
- extern int (*_ONBREAK)();
-
- /**************************************************************************/
-
- int CXBRK()
- {
- CodeErreur=BreakError;
-
- return(0);
- }
-
- /**************************************************************************/
-
- void InsererMot()
- {
- /* VARIABLES LOCALES */
- register UBYTE Pos=0;
-
- Car=fgetc(FIn);
-
- /* Chargement dans la liste et écriture dans le fichier décompressé du
- * nouveau mot.
- * Le nouveau mot se termine dans le fichier lu, par la marque de nouveau
- * mot NouveauMot='\0'
- */
- while((FIn->_flag)==_IOREAD && CodeErreur==0 && Car!=NouveauMot)
- {
- if(fputc(ListeMots[NbMots][Pos++]=Car,FOut)==EOF) CodeErreur=WriteError;
- Car=fgetc(FIn);
- }
-
- /* Erreur en lecture ? */
- if(((FIn->_flag)!=_IOREAD || Car) && CodeErreur==0) CodeErreur=ReadError;
-
- ListeMots[NbMots][Pos]=0;
-
- NbMots++;
- }
-
- /**************************************************************************/
-
- UBYTE DeCompresserFichier()
- {
- /* Le fichier est-il vide ? */
- /* if(((Car=fgetc(FIn))==EOF) && (feof(FIn))) */
- /* { */
- /* printf("Fichier vide.\n"); */
- /* return(1); */
- /* } */
- /* */
- /* Fichier non vide : on replace le caractère lu dans le buffer */
- /* ungetc(Car,FIn); */
-
- printf("Création du fichier %s ",NomFicOut);
-
- if((FOut=fopen(NomFicOut,"wb"))==NULL) return(CreateError);
-
- CarPrec='\n';
- Car=fgetc(FIn);
-
- ComptLignes=NbMots=0;
-
- while(FIn->_flag==_IOREAD && CodeErreur==0)
- {
- /* Code d'une ligne vide */
- if(Car==LigneVide){
- if(fputc('\n',FOut)==EOF) CodeErreur=WriteError;
- }
- else{
- /* Ecriture des espaces initiaux, Car=Nb espaces initiaux */
- while(Car && CodeErreur==0)
- {
- if(fputc(' ',FOut)==EOF) CodeErreur=WriteError;
- Car--;
- }
-
- /* Lecture du premier caractère ou code */
- Car=fgetc(FIn);
-
- /* Lecture du restant de la ligne compressée */
- while(FIn->_flag==_IOREAD && Car!='\n' && CodeErreur==0)
- {
- /* Code d'un nouveau mot */
- if(Car==NouveauMot) InsererMot();
-
- /* Code d'un mot déjà lu */
- else if(Car==MotExistant)
- {
- /* Lecture du code du mot */
- CodeMot=fgetc(FIn)|(fgetc(FIn)<<8);
-
- /* Ecriture du mot */
- if(fputs(ListeMots[CodeMot],FOut)) CodeErreur=WriteError;
- }
-
- /* Code d'un début de commentaire */
- else if(Car==Commentaire1On)
- {
- if(fputs("/*",FOut)) CodeErreur=WriteError;
- }
-
- /* Code d'une fin de commentaire */
- else if(Car==Commentaire1Off)
- {
- if(fputs("*/",FOut)) CodeErreur=WriteError;
- }
-
- /* Code d'un début de commentaire + espace */
- else if(Car==Commentaire2On)
- {
- if(fputs("/* ",FOut)) CodeErreur=WriteError;
- }
-
- /* Code d'un espace + fin de commentaire */
- else if(Car==Commentaire2Off)
- {
- if(fputs(" */",FOut)) CodeErreur=WriteError;
- }
-
- /* Caractère quelconque */
- else if(fputc(Car,FOut)==EOF) CodeErreur=WriteError;
-
- Car=fgetc(FIn);
- }
-
- /* Saut de fin de ligne */
- if(fputc('\n',FOut)==EOF) CodeErreur=WriteError;
-
- /* Affichage du nombre d'octets lus */
- if(!ComptLignes++) printf("%7u\b\b\b\b\b\b\b",ftell(FIn));
-
- ComptLignes%=10;
- }
-
- Car=fgetc(FIn);
- }
-
- if(CarPrec!='\n' && CodeErreur==0) if(fputc('\n',FOut)==EOF) CodeErreur=WriteError;
-
- /* Erreur en lecture ou fin normal de fichier ? */
- if(FIn->_flag!=(_IOREAD|_IOEOF)) CodeErreur=ReadError;
-
- return(CodeErreur);
- }
-
- /**************************************************************************/
-
- UBYTE main(int argc,char *argv[])
- {
- /* Déclarations et fonctions de Exec.Library */
- extern struct ExecBase *SysBase;
- extern void *AllocMem(long,long);
- extern void FreeMem(void *,long);
- #pragma syscall AllocMem c6 1002
- #pragma syscall FreeMem d2 902
-
- register struct FileInfoBlock *PtrFIB;
-
- /* Déclarations de variables externes de la DOS.Library */
- extern struct DosLibrary *DOSBase;
- extern BPTR CreateDir(char *);
- extern long DeleteFile(char *);
- extern long SetProtection(char *, ULONG);
- extern LONG SetComment(char *, char *);
- extern LONG Examine(BPTR, struct FileInfoBlock *);
- extern BPTR Lock(char *, LONG);
- extern void UnLock(BPTR);
- #pragma libcall DOSBase CreateDir 78 101
- #pragma libcall DOSBase DeleteFile 48 101
- #pragma libcall DOSBase SetProtection ba 2102
- #pragma libcall DOSBase SetComment b4 2102
- #pragma libcall DOSBase Examine 66 2102
- #pragma libcall DOSBase Lock 54 2102
- #pragma libcall DOSBase UnLock 5a 101
-
- /* VARIABLES LOCALES */
- register UBYTE NuArg,
- FlagNewDir=0, /* Changer le répertoire de destination ? */
- CreerChemin=0, /* Créer le nouveau répertoire ? */
- NewChemin[80],
- FileName[108],
- Commentaire[80];
-
- register BPTR Clef; /* Paramètre utilisé pour Examine() et CreateDir() */
-
- register ULONG Protection;
-
- /*********************** DEBUT DU CODE DE main() **************************/
-
- _ONBREAK=CXBRK;
-
- printf("DeCompC V1.00 FREEWARE Christian BRUNON 20-08-1993\n");
-
- /* Pas de paramètre ou paramètre ? */
- if(argc==1 || (argc==2 && argv[1][0]=='?' && argv[1][1]=='\0'))
- {
- printf("%s",ListeErreurs[1]);
- return(1);
- }
-
- if((PtrFIB=(struct FileInfoBlock *)AllocMem(sizeof(struct FileInfoBlock),0L))==NULL)
- {
- printf("%s",ListeErreurs[MemoryError]);
- return(MemoryError);
- }
-
- for(NuArg=1;NuArg<argc;NuArg++)
- if(argv[NuArg][0]=='-')
- /* Le paramètre lu est un nouveau chemin */
- {
- FlagNewDir=(UBYTE)1;
- strcpy(NewChemin,&argv[NuArg][1]);
-
- if(NewChemin[0]) CreerChemin=LastChar(NewChemin)!=':' && LastChar(NewChemin)!='/';
-
- if(CreerChemin && NewChemin[0])
- {
- if(Clef=CreateDir(NewChemin)) UnLock(Clef);
-
- strcat(NewChemin,"/");
- }
- }
- /* Le paramètre lu est un fichier à décompresser */
- else{
- CodeErreur=0;
-
- /* Détermination du nom du fichier à décompresser.
- * Rajout de l'extension ".K" si nécessaire.
- */
- strcpy(NomFicIn,argv[NuArg]);
- if(NomFicIn[strlen(NomFicIn)-2]!='.' || UpChar(LastChar(NomFicIn))!='K') strcat(NomFicIn,".K");
-
- if(Clef=Lock(NomFicIn,ACCESS_READ))
- {
- /* Test existence et test type = fichier */
- if(Examine(Clef,PtrFIB) && PtrFIB->fib_DirEntryType<0)
- {
- strcpy(FileName,PtrFIB->fib_FileName);
- LongFicIn=PtrFIB->fib_Size;
- Protection=(ULONG)PtrFIB->fib_Protection;
- strcpy(Commentaire,PtrFIB->fib_Comment);
- }
- else CodeErreur=FileOpenReadError;
-
- UnLock(Clef);
- }
- else CodeErreur=FileOpenReadError;
-
- if(CodeErreur){
- printf("%s %s",NomFicIn,ListeErreurs[CodeErreur]);
- continue;
- }
-
- /* Détermination du nom du fichier décompressé */
- if(FlagNewDir)
- {
- strcpy(NomFicOut,NewChemin);
- strcat(NomFicOut,FileName);
- }
- else strcpy(NomFicOut,NomFicIn);
-
- /* Annulation des 2 derniers caractères qui sont ".K" */
- NomFicOut[strlen(NomFicOut)-2]='\0';
-
- printf("Ouverture du fichier %s\r",NomFicIn);
-
- if(FIn=fopen(NomFicIn,"rb"))
- {
- CodeErreur=DeCompresserFichier();
-
- /* Affichage du code d'erreur */
- if(CodeErreur) printf("%s",ListeErreurs[CodeErreur]);
- else printf("%7u\n",ftell(FOut));
-
- if(FOut) fclose(FOut);
- if(FIn) fclose(FIn);
-
- if(CodeErreur) DeleteFile(NomFicOut);
- else /* Recopie des bits d'attributs de fichier et du commentaire */
- {
- SetProtection(NomFicOut,Protection);
- SetComment(NomFicOut,Commentaire);
- }
- }
- /* Fichier inexistant ou illisible */
- else printf("%s",ListeErreurs[CodeErreur=FileOpenReadError]);
- }
-
- if(PtrFIB) FreeMem(PtrFIB,sizeof(struct FileInfoBlock));
-
- /* Renvoie le code d'erreur du dernier fichier décompressé */
- return(CodeErreur);
- }
-